<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/9/24
 * Time: 9:38
 */

namespace app\admin\controller;

use app\admin\model\AreaModel;
use think\Validate;
use cmf\controller\AdminBaseController;
use app\admin\model\SchoolModel;
use think\Db;
use think\db\Query;

class SchoolController extends AdminBaseController
{
    public function index()
    {
        $name = $this->request->param('name');
        $data = Db::name('school s')
            ->where(function (Query $query) use ($name){
                if ($name){
                    $query ->where('name','like',"%$name%");
                }
            })
            ->join('ji_area a','a.areaId = s.province')
            ->join('ji_area b','b.areaId = s.city')
            ->join('ji_area c','c.areaId = s.area')
            ->field('s.*,a.areaName a,b.areaName b,c.areaName c')
            ->order('id desc')
            ->paginate(3,false,$this->request->param());
        $data->appends(['name'=>$name]);
        $page = $data->render();
        return view('',['school'=>$data,'page'=>$page]);
    }
    public function getCity()
    {
        $areaId = input('areaId');
        $city = Db::name('area')->where('parentId',$areaId)->where('level',2)->select();
        $cityArr = ["<option value=''>请选择城市</option>"];
        foreach ($city as $v){
            $opt = "<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr,$opt);
        }
        //返回$data
        return $cityArr;

    }
    public function getArea()
    {
        $cityId = input('areaId');
        $area = Db::name('area')->where('parentId',$cityId)->where('level',3)->select();
        $cityArr = ["<option value=''>请选择县区</option>"];
        foreach ($area as $v){
            $opt = "<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr,$opt);
        }
        return $cityArr;

    }

    public function add()
    {
        $province = Db::name('area')->where('level',1)->select();
        return view('',['province'=>$province]);
    }
    public function addPost()
    {
        $data = input();
        $rule = [
            'name' => 'require',
            'province' => 'require',
            'city' => 'require',
            'area' => 'require'
        ];
        $msg = [
            'name.require' => '学校名称不能为空',
            'province.require' => '请选择省份',
            'city.require' => '请选择城市',
            'area.require' => '请选择县区'
        ];
        $validate = Validate::make($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }
        $add = SchoolModel::create($data);
        if ($add){
            $this->success('添加成功','school/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit()
    {
        $id = input();
        $school = Db::name('school')->get($id);
        $province = Db::name('area')->where('level',1)->all();
        $city = Db::name('area')->where('level',2)->where('parentId',$school['province'])->all();
        $area = Db::name('area')->where('level',3)->where('parentId',$school['city'])->all();
        return $this->fetch('', [
            'school' => $school,
            'province' => $province,
            'city' => $city,
            'area' => $area,
        ]);
    }
    public function editPost()
    {
        $data = input();
        if (empty($data['name'])){
            $this->error('学校名不能为空');
        }
        $edit = SchoolModel::update($data,['id'=>$data['id']]);
        if ($edit){
            $this->success('修改成功','school/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete()
    {
        $id=input('id');
        $del=SchoolModel::destroy($id);
        if ($del){
            $this->success("删除成功！", url("school/index"));
        }else{
            $this->error("删除失败！");
        }
    }
}